package com.gitee.feizns.explore.data_structure.recursive.memoization;

/**
 * @author feizns
 * @since 2019/12/10
 */
public class Pow {

    public static void main(String[] args) {
        System.out.println(myPow(2, -2));
    }

    public static double myPow(double x, int n) {
        return n > 0 ? pow(x, n) : 1 / pow(x, n);
    }

    private static double pow(double x, int n) {
        if ( n == 0 )
            return 1;
        double pow = pow(x, n / 2);
        return n % 2 == 0 ? pow * pow : x * pow * pow;
    }

}
